import logging
from html import parser

import requests

from api.p2p_reg_login import RegLogin
from api.tender import Tender
from common.parser import parser_html


class TestTender(object):
    def setup(self):
        self.session = requests.Session()
        self.reg_login = RegLogin(session = self.session)
        self.reg_login.login({"keywords": "13800005001", "password": "Aa123456"})
        self.ten = Tender(self.session)


    def test01_tender_success(self):
        data = {"id": 714,
            "depositCertificate": -1,
            "amount": 100}
        resp = self.ten.tender(data)
        logging.info(f"后台投资的结果是:{resp.json()}")
        # 断言结果
        assert 200 == resp.status_code
        assert 200 == resp.json().get("status")
        assert "form" in resp.json().get("description")


    def test02_tender_no_amount(self):
        data = {"id": 714,
            "depositCertificate": -1,
            "amount": ""}
        resp = self.ten.tender(data)
        logging.info(f"后台投资的结果是:{resp.json()}")
        # 断言结果
        assert 200 == resp.status_code
        assert 100 == resp.json().get("status")
        assert "不是正确的金额" in resp.json().get("description")


    def test03_tender_no_password(self):
        data = {"id": 715,
            "depositCertificate": -1,
            "amount": "100","password" : ""}
        resp = self.ten.tender(data)
        logging.info(f"后台投资的结果是:{resp.json()}")
        # 断言结果
        assert 200 == resp.status_code
        assert 100 == resp.json().get("status")
        assert "密码不能为空" in resp.json().get("description")


    def test04_third_tender(self):
        data = {"id": 714,
            "depositCertificate": -1,
            "amount": "100"}
        resp = self.ten.tender(data)
        result = parser_html(resp)
        r = requests.post(url = result[0],data = result[1])
        logging.info(f"后台投资的结果是:{r.text}")
        # 断言结果
        assert "OK" in r.text







        # session = requests.Session()
        # data = {"id": 714,
        #     "depositCertificate": -1,
        #     "amount": 100}
        # reg_login = RegLogin(session)
        # ten = TestTender(session)
        # data1 = {"keywords": "13800005001", "password": "Aa123456"}
        # reg_login.login(data1)
        # resp=ten.test01_tender(data)
        # logging.info(f"后台投资的结果是:{resp.json()}")
        # # 断言结果
        # assert 200 == resp.status_code
        # assert 200 == resp.json().get("status")
        # assert "form" in resp.json().get("description")


